package com.example.demo.util.httpclient;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class RestTemplateUtils {

    public String get(String url, String env) {
        RestTemplate restTemplate = new RestTemplate();
        HttpsVerify.init();
        return restTemplate.exchange(url, HttpMethod.GET, buildHttpEntity(env), String.class).getBody();
    }

    public HttpHeaders buildHttpHeaders(String env) {
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("IS_ADMIN", "true");
        requestHeaders.add("REQUESTION_SOURCE", "ALC");
        String apiKey = "";
        if ("dev".equalsIgnoreCase(env)) {
            apiKey = "l7abc76e9841fa4ddc834d39025fd8f9bd";
        } else if ("uat".equalsIgnoreCase(env)) {
            apiKey = "l7abc76e9841fa4ddc834d39025fd8f9bd";
        } else if ("pre".equalsIgnoreCase(env)) {
            apiKey = "l7abc76e9841fa4ddc834d39025fd8f9bd";
        } else if ("pro".equalsIgnoreCase(env)) {
            apiKey = "l7f7d748f32cad48aa851f6133b64be601";
        }

        requestHeaders.add("apikey", apiKey);
        return requestHeaders;
    }

    public HttpEntity<String> buildHttpEntity(String env) {
        return new HttpEntity<>(null, buildHttpHeaders(env));
    }

}
